#include "qrectproperty.h"

#include "qintproperty.h"

#include "../../libs/sharedLib/qeditorlabel.h"

QRectProperty::QRectProperty(SHAREDLIB::QAbstractProperty *parent):
    SHAREDLIB::QAbstractProperty(parent)
{

    m_x=new QIntProperty(this);
    m_x->setAttribute("property_name","x");
    m_x->setAttribute("name","x");

    m_y=new QIntProperty(this);
    m_y->setAttribute("property_name","y");
    m_y->setAttribute("name","y");

    m_width=new QIntProperty(this);
    m_width->setAttribute("property_name","Width");
    m_width->setAttribute("name","Width");

    m_height=new QIntProperty(this);
    m_height->setAttribute("property_name","Height");
    m_height->setAttribute("name","Height");
}

QWidget* QRectProperty::getEditor()
{
    if(attribute("editabled").toBool())
    {
        m_editor=new SHAREDLIB::QEditorLabel(this);
    }
    return QAbstractProperty::getEditor();
}

void QRectProperty::setValue(const QVariant &value)
{
    QRect re=value.toRect();
    if(m_x->attribute("visible").toBool())
    {
        m_x->setValue(re.x());
        m_y->setValue(re.y());
    }
    else
    {
        m_x->setValue(0);
        m_y->setValue(0);
        re=QRect(0,0,re.width(),re.height());
    }
    m_width->setValue(re.width());
    m_height->setValue(re.height());

    SHAREDLIB::QAbstractProperty::setValue(value);
}

QVariant QRectProperty::getValue()
{
    QVariant v=SHAREDLIB::QAbstractProperty::getValue();
    if(!m_x->attribute("visible").toBool())
    {
        QRect rect=v.toRect();
        rect.setRect(0,0,rect.width(),rect.height());
        v.setValue<QRect>(rect);
    }
    return v;
}

void QRectProperty::subPropertyChanged(const QVariant &)
{
    QRect re;
    re.setX(m_x->getValue().toInt());
    re.setY(m_y->getValue().toInt());
    re.setWidth(m_width->getValue().toInt());
    re.setHeight(m_height->getValue().toInt());
    setValue(re);
}

QString QRectProperty::valueText()
{
    if(m_x->attribute("visible").toBool())
    {
        return QString("[(%1,%2),%3 x %4]")
            .arg(m_x->getValue().toInt())
            .arg(m_y->getValue().toInt())
            .arg(m_width->getValue().toInt())
            .arg(m_height->getValue().toInt());
    }
    else
    {
        return QString("%1 x %2").arg(m_width->getValue().toInt()).arg(m_height->getValue().toInt());
    }
}
